import 'package:flutter/material.dart';
import '../res/listData.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Flutter List", style: TextStyle(color: Colors.white)),
          backgroundColor: Colors.blue,
        ),
        body: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  MyHomePage({super.key}) {
    print(listData);
  }
  // 第一种方法
  // List<Widget> _initListData() {
  //   List<Widget> tempList = [];
  //   for (int i = 0; i < listData.length; i++) {
  //     tempList.add(
  //       ListTile(
  //         leading: Image.network("${listData[i]["imageUrl"]}"),
  //         title: Text("${listData[i]["title"]}"),
  //         subtitle: Text("${listData[i]["author"]}"),
  //       ),
  //     );
  //   }
  //   return tempList;
  // }
  // 第二种方法
  List<Widget> _initListData() {
    var tempList = listData.map((value) {
      return ListTile(
        leading: Image.network("${value["imageUrl"]}"),
        title: Text("${value["title"]}"),
        subtitle: Text("${value["author"]}"),
      );
    });

    return tempList.toList();
  }

  @override
  Widget build(BuildContext context) {
    return ListView(children: _initListData());
  }
}
